package com.clouddong.exterapi.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.clouddong.exterapi.po.ProbeListTablePo;
import com.clouddong.exterapi.service.ProbeListServiceI;
import com.clouddong.exterapi.service.ProbeTaskServiceI;
import com.clouddong.exterapi.vo.ProbeListTableVo;
import com.wordnik.swagger.annotations.ApiOperation;

/**
 * <p>
 * Title:ProbeTaskAddController
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Company:
 * </p>
 * 
 * @author wyx
 * @date 2018年6月6日 下午3:35:11
 */
@Controller
@RequestMapping("/probeTask")
@CrossOrigin(origins="*")
public class ProbeTaskAddController
{
	@Autowired
	public ProbeTaskServiceI proServiceI;
	@Autowired
	public ProbeListServiceI probeListServiceI;
	private final static Logger logger = LoggerFactory.getLogger(ProbeTaskAddController.class);

	@RequestMapping(value="/manualTestUploadData",method= {RequestMethod.GET},produces="application/json;charset=utf-8")
	@ApiOperation(value = "手工测试数据上报", notes = "upload data", httpMethod = "GET")
	@ResponseBody
	public void manualTestUploadData(HttpServletRequest request)
	{
	    String url=request.getSession().getServletContext().getRealPath("")+"probeTestXML.xml";//要生成的路径
	    proServiceI.probeTestXmlOutUrl(url);//自动根据表自动生成测试XML并返回是否创建成功
		proServiceI.probeXmlReader(url,request);
	}

	@RequestMapping(value="/synchronizationByCondition",method= {RequestMethod.GET},produces="application/json;charset=utf-8")
	@ApiOperation(value = "同步探针数据表", notes = "get probeList", httpMethod = "GET")
	@ResponseBody
	public void synchronizationByCondition()
	{
	    System.out.println("========================执行了"+Thread.currentThread().getStackTrace()[1].getMethodName()+"方法==============================");
		probeListServiceI.synchronizationByCondition();
	}
	@RequestMapping(value="/probeList",method= {RequestMethod.GET},produces="application/json;charset=utf-8")
	@ApiOperation(value = "获探针数据列表", notes = "get probeList", httpMethod = "GET")
	@ResponseBody
	public List<ProbeListTablePo> getProbeList(@RequestParam(value = "id", required = false) String id,
			@RequestParam(value = "province", required = false) String province,
			@RequestParam(value = "city", required = false) String city)
	{
		ProbeListTableVo probeListTableVo = new ProbeListTableVo();
		if ((id != null) && (id != ""))
		{
			probeListTableVo.setProbe_id(id);
		}
		if ((province != null) && (province != ""))
		{
			probeListTableVo.setProbe_province(province);
		}
		if ((city != null) && (city != ""))
		{
			probeListTableVo.setProbe_city(city);
		}
		List<ProbeListTablePo> probeListTablePos = probeListServiceI.getProbeDataList(probeListTableVo);
		return probeListTablePos;

	};

	/**
	 * @author xuan
	 * @param tasktypeid
	 *            //测试类型编号 0--封堵方式(实质上也是ping方式，只是区分开展示颜色),1--ping,2--http,3--flash,4--dns,5--trace
	 * @param destaddr
	 *            //测试的 ip或域名
	 * @param proids
	 *            //探针id串，null为全网
	 * 
	 */
	@RequestMapping(value="/probeAdd",method= {RequestMethod.POST},produces="application/json;charset=utf-8")
	@ApiOperation(value = "探针任务新增", notes = "get probeList", httpMethod = "POST")
	@ResponseBody
	@Transactional(rollbackFor=Exception.class)
	public Object probeAdd(@RequestParam(value = "tasktypeid") String tasktypeid,
						   @RequestParam(value = "destaddr") String destaddr,
						   @RequestParam(value = "proids", required = false) String proids,
			               @RequestParam(value="rule", required = false) String rule)
	{
			return proServiceI.probeAdd(tasktypeid, destaddr, proids,rule);
	}

	@RequestMapping(value="/probeDel",method= {RequestMethod.POST},produces="application/json;charset=utf-8")
	@ApiOperation(value = "探针任务删除", notes = "del probeList", httpMethod = "POST")
	@ResponseBody
	@Transactional(rollbackFor=Exception.class)
	public Object probeDel(@RequestParam(value = "taskids") String taskids)
	{
	    System.out.println("========================================进入了probeDel方法========================================");
		return proServiceI.probeDel(taskids);

	}

	@RequestMapping(value="/probeData",method= {RequestMethod.POST},produces="application/json;charset=utf-8")
	@ApiOperation(value = "探针任务新增后返回的数据", notes = "get probeListData", httpMethod = "POST")
	@ResponseBody
	public void probeData(HttpServletRequest request, HttpServletResponse response)
	{
		try
		{
			proServiceI.probeData(request, "");
		} catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// return null;
	}

	@RequestMapping(value="/probeDelData",method= {RequestMethod.POST},produces="application/json;charset=utf-8")
	@ApiOperation(value = "探针任务删除后返回的数据", notes = "get probeListData", httpMethod = "POST")
	@ResponseBody
	public void probeDelData()
	{
		System.out.println("返回接收");

	}

}
